﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SurveySystem.Core;

namespace SurveySystem.Web
{
    public partial class ShowSurvey : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

             int surveyId = 0;
             bool s = Int32.TryParse(Request.QueryString["surveyId"], out surveyId);
             if (s == false)
             {
                 Response.Redirect("./Default.aspx");
             }
             if (!SurveysBLL.HasAccess(surveyId,User.Identity.Name))
             {
                 Response.Redirect("./Default.aspx");
             }
             Survey survey = SurveysBLL.GetSurvey(surveyId);
             questionLabel.Text = survey.question;
             IEnumerable<Answer> answers = SurveysBLL.GetMyAnswers(surveyId, User.Identity.Name).ToList();
             if (!survey.isExpired)
             {
                 ResultView.Visible = false;
                 if (!IsPostBack)
                 {
                     VoteView.Visible = true;
                     switch (survey.surveyType)
                     {
                         case SurveyType.Free:
                             if (answers.Count() == 1 && answers.First() is FreeAnswer)
                             {
                                 freeTextBox.Text = ((FreeAnswer)answers.First()).text;
                             }
                             VoteView.SetActiveView(TextView);
                             break;
                         case SurveyType.Choice:
                             VoteView.SetActiveView(SingleSelectView);
                             break;
                         case SurveyType.MultiChoice:
                            
                             VoteView.SetActiveView(MultiSelectView);
                             break;

                     }
                     if (answers.Count() > 0) sendButton.Text = "Módosít";
                 }
                 else
                 {
                     if (survey.surveyType == SurveyType.Free)
                     {
                         if (freeTextBox.Text.Trim().Length == 0)
                         {
                             VoteView.Visible = true;
                             textLengthValidator.Visible = true;
                             return;
                         }
                         else
                         {
                             SurveysBLL.AddFreeAnswer(survey.id, User.Identity.Name, freeTextBox.Text.Trim());
                             //FreeAnswer answer = new FreeAnswer();
                             //answer.text = freeTextBox.Text.Trim();
                             //answer.surveyId = survey.id;
                             //answer.userId = UsersBLL.GetUser(User.Identity.Name).id;
                             //SurveysBLL.AddFreeAnswer(answer);
                         }
                     }
                     if (survey.surveyType == SurveyType.Choice)
                     {
                         if (SingleBox.SelectedIndex < 0)
                         {
                             VoteView.Visible = true;
                             singleSelectValidator.Visible = true;
                             return;
                         }
                         else
                         {
                             //ChoiceAnswer c = new ChoiceAnswer();
                             //c.userId = UsersBLL.GetUser(User.Identity.Name).id;
                             //c.surveyId = survey.id;
                             //SurveysBLL.AddChoiceAnswer
                             int choiceId;
                             if (Int32.TryParse(SingleBox.SelectedValue, out choiceId))
                             {
                                 SurveysBLL.AddChoiceAnswer(survey.id, User.Identity.Name, choiceId);
                             }
                         }
                     }
                     if (survey.surveyType == SurveyType.MultiChoice)
                     {
                         //List<ChoiceAnswer> l = new List<ChoiceAnswer>();
                         List<int> choiceIds = new List<int>();
                         User u = UsersBLL.GetUser(User.Identity.Name);
                         for (int i = 0; i < MultiBox.Items.Count; i++)
                         {
                             if (MultiBox.Items[i].Selected)
                             {
                                 int choiceId;
                                 if (Int32.TryParse(MultiBox.Items[i].Value, out choiceId))
                                 {
                                     choiceIds.Add(choiceId);
                                 }
                                 
                                 //ChoiceAnswer c = new ChoiceAnswer();
                                 //c.userId = u.id;
                                 //c.surveyId = survey.id;
                                 //int x;
                                 //Int32.TryParse(MultiBox.Items[i].Value, out x);
                                 //c.choiceId = x;
                                 //l.Add(c);
                             }
                         }
                         if (choiceIds.Count > 0)
                         {
                             SurveysBLL.AddMultiAnwers(survey.id, User.Identity.Name, choiceIds);
                         }
                         else
                         {
                             VoteView.Visible = true;
                             multiSelectValidator.Visible = true;
                             return;
                         }
                     }
                     Response.Redirect("~/Default.aspx");
                 }
             }
             else
             {
                 ResultView.Visible = true;
                 VoteView.Visible = false;
                 sendButton.Text = "Vissza";
                 if (IsPostBack)
                 {
                     Response.Redirect("~/Default.aspx");
                 }
                 switch (survey.surveyType)
                 {
                     case SurveyType.Free:
                         ResultView.SetActiveView(resTextView);
                         break;
                     case SurveyType.Choice:
                         ResultView.SetActiveView(resSingleSelectView);
                         break;
                     case SurveyType.MultiChoice:
                         ResultView.SetActiveView(resMultiSelectView);
                         break;

                 }
             
             }
        }   
        
        protected void resMultiView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
           AnswerSummary summary = e.Row.DataItem as AnswerSummary;
           int surveyId;
           if (summary != null)
           {
               Int32.TryParse(Request.QueryString["surveyId"], out surveyId);
               IEnumerable<Answer> a = SurveysBLL.GetMyAnswers(surveyId, User.Identity.Name);
               foreach (ChoiceAnswer c in a)
               {
                   if (c.choiceId == summary.AnswerId)
                   {
                       e.Row.Font.Bold = true;
                   }
               }
           }
        }

        protected void MultiBox_DataBound(object sender, EventArgs e)
        {
            CheckBoxList ch = sender as CheckBoxList;
            int surveyId;
            bool s = Int32.TryParse(Request.QueryString["surveyId"], out surveyId);
            IEnumerable<Answer> answers = SurveysBLL.GetMyAnswers(surveyId, User.Identity.Name);
            foreach (Answer a in answers)
            {
                if (a is ChoiceAnswer)
                {
                    ChoiceAnswer c = (ChoiceAnswer)a;
                    for (int i = 0; i < ch.Items.Count; i++)
                    {
                        if (ch.Items[i].Value == c.choiceId.ToString())
                        {
                           ch.Items[i].Selected = true;
                        }
                    }
                }
            }
        }

        protected void MultiBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void SingleBox_DataBound(object sender, EventArgs e)
        {
            RadioButtonList b = sender as RadioButtonList;
            int surveyId;
            bool s = Int32.TryParse(Request.QueryString["surveyId"], out surveyId);
            IEnumerable<Answer> answers = SurveysBLL.GetMyAnswers(surveyId, User.Identity.Name);
            foreach (Answer a in answers)
            {
                if (a is ChoiceAnswer)
                {
                    ChoiceAnswer c = (ChoiceAnswer)a;
                    for (int i = 0; i < b.Items.Count; i++)
                    {
                        if (b.Items[i].Value == c.choiceId.ToString())
                        {
                            b.Items[i].Selected = true;
                        }
                    }
                }
            }
        }
    }
}
